﻿using Commission.Models;
using Commission.ViewModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Commission.Helpers
{
    public class DataTableUtil
    {
        public static DataTable ListToDataTable(IEnumerable<TransactionHistory> data)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add(Resources.AppResource.LabelUserName, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelName, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelCustomerType, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelAccountType, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelTransactionDate, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelPoint, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelAmount80, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelAmount20, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelTotal, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelDescription, Type.GetType("System.String"));

            foreach (TransactionHistory item in data)
            {
                DataRow dr = dt.NewRow();
                dr[0] = item.Customer.UserName;
                dr[1] = item.Customer.Name;
                dr[2] = item.Customer.CustomerType.FullTypeName;
                dr[3] = item.AccountType.ToString();
                dr[4] = item.AccountDate.ToString("dd/MM/yyyy HH:mm");
                dr[5] = item.Point;
                dr[6] = item.Amount;
                dr[7] = item.ProductAmount;
                dr[8] = item.TotalAmount;
                dr[9] = item.Description;

                /*
               
                dr[2] = item.sap_id;
                dr[3] = item.UnitPrice;
                dr[4] = item.doc_id;
                dr[5] = item.return_id;
                dr[6] = item.Serial;
                dr[7] = item.LineNo;
                dr[8] = item.PartNo;
                 */
                dt.Rows.Add(dr);
            }
            dt.AcceptChanges();

            return dt;
        }

        public static DataTable ListToDataTable(IEnumerable<TransactionHistorySum> data)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add(Resources.AppResource.LabelUserName, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelName, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelCustomerType, Type.GetType("System.String"));
            
            dt.Columns.Add(Resources.AppResource.LabelDirectAmount, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelRollupAmount, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelPairingAmount, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelOnlineAmount, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelMatchingAmount, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelUsedAmount, Type.GetType("System.String"));

            dt.Columns.Add(Resources.AppResource.LabelTotal, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelAmount80, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelAmount20, Type.GetType("System.String"));
            dt.Columns.Add(Resources.AppResource.LabelRemain, Type.GetType("System.String"));

            foreach (TransactionHistorySum item in data)
            {
                DataRow dr = dt.NewRow();
                dr[0] = item.Customer.UserName;
                dr[1] = item.Customer.Name;
                dr[2] = item.Customer.CustomerType.FullTypeName;

                dr[3] = item.DirectAmount;
                dr[4] = item.RollupAmount;
                dr[5] = item.PairingAmount;
                dr[6] = item.OnlineAmount;
                dr[7] = item.MatchingAmount;
                dr[8] = item.UsedAmount;

                dr[9] = item.TotalAmount;
                dr[10] = item.TotalAmount80;
                dr[11] = item.TotalAmount20;
                dr[12] = item.CumulativeAmount;
               
                dt.Rows.Add(dr);
            }
            dt.AcceptChanges();

            return dt;
        }
    }
}